<template>
    <view>
        <!-- pages/pointsMall/pointsMall.wxml -->
        <view class="pointsMall" v-if="mallList.length > 0">
            <view class="pointsMall-item" @tap="goDetail" :data-item="item" v-for="(item, index) in mallList" :key="index">
                <image :src="item.pic" mode="widthFix"></image>

                <view class="pointsMall-item-title">
                    {{ item.name }}
                </view>

                <view class="pointsMall-item-info">
                    <view class="pointsMall-item-info-points">{{ item.point }}</view>
                    <view class="pointsMall-item-info-tip">积分兑换</view>
                    <view class="pointsMall-item-info-num">已兑换{{ item.exchangeNum }}件</view>
                </view>
            </view>
        </view>
        <noData v-else />
    </view>
</template>

<script>
// pages/pointsMall/pointsMall.js
const app = getApp();

const api = require('../../utils/api.js');
var common =require('../../utils/util.js');

export default {
    data() {
        return {
            // mallList:[{title:'蓝月亮洗衣液3kg薰衣草整箱批家用实惠装',src:'',points:'4699',num:'20111'},
            // {title:'NONOO吸管杯KAKAO可爱网红大人高颜值儿童学',src:'',points:'899',num:'321'},
            // {title:'pvc新款红色PU手袋透明果冻包炫彩镭射手提袋',src:'',points:'299',num:'121'}],//模拟数据
            mallList: [],
            pageNumber: 1,
            totalPages: 1
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.getList();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        //调用刷新时将执行的方法
        this.onRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.pageNumber + 1 <= this.totalPages) {
            // 若不是最后一页，则将当前页数增加1，获取数据
            this.setData({
                pageNumber: this.pageNumber + 1
            });
            this.getList();
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        goDetail(event) {
            // console.log(event)
            let detail = event.currentTarget.dataset.item; // wx.setStorageSync('giftDetail', JSON.stringify(detail))

            uni.navigateTo({
                url: '../pointsMallDetail/pointsMallDetail?giftId=' + detail.id
            });
        },

        getList() {
            let identityId = uni.getStorageSync('identityId');
            uni.$get(api.giftList + '?identityId=' + identityId + '&pageNumber=' + this.pageNumber + '&pageSize=10')
                .then((res) => {
                    //请求成功
                    if (res.success == true) {
                        if (res.data.content && res.data.content.length > 0) {
                            let list = [];

                            if (this.pageNumber == 1) {
                                // 若请求第一页，则直接赋值
                                list = res.data.content;
                                this.setData({
                                    totalPages: res.data.totalPages
                                });
                            } else {
                                // 否则将获取的数据与原数组合并
                                list = this.mallList.concat(res.data.content);
                            }

                            this.setData({
                                mallList: list
                            });
                        }
                    } else {
                        this.setData({
                            mallList: []
                        });
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        });
                    }

                    uni.hideNavigationBarLoading(); //完成停止加载

                    uni.stopPullDownRefresh(); //停止下拉刷新
                })
                .catch((err) => {
                    //请求失败
                });
        },

        //刷新
        onRefresh() {
            //在当前页面显示导航条加载动画
            uni.showNavigationBarLoading();
            this.setData({
                pageNumber: 1
            });
            this.getList();
        }
    }
};
</script>
<style>
@import './pointsMall.css';
</style>
